﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

#if NETFX_CORE
using Windows.ApplicationModel.Resources;

#endif

namespace StyleMVVM.Validation.Impl.Fluent.Operators
{
	public class IsEmptyOperator<T> : UnaryOperator<T>
	{
		public IsEmptyOperator()
		{
			expressionMethod = GetType().GetTypeInfo().GetDeclaredMethod("Operate");
		}

#if NETFX_CORE
		public override string GenerateLogicStatement(ResourceLoader fluentResources)
		{
			return InternalGenerateMessage(fluentResources, "IsEmpty");
		}

		public override string GenerateErrorMessage(ResourceLoader fluentResources)
		{
			return InternalGenerateMessage(fluentResources, "MustBeEmpty");
		}
#else
		public override string GenerateLogicStatement()
		{
			return InternalGenerateMessage(FluentResources.IsEmpty);
		}

		public override string GenerateErrorMessage()
		{
			return InternalGenerateMessage(FluentResources.MustBeEmpty);
		}
#endif

		public static bool Operate(object leftValue)
		{
			if (leftValue == null)
			{
				return true;
			}

			if (Equals(leftValue, string.Empty))
			{
				return true;
			}

			IEnumerable enumerable = leftValue as IEnumerable;

			if (enumerable != null)
			{
				foreach (object o in enumerable)
				{
					return false;
				}

				return true;
			}

			return false;
		}
	}
}